//
//  FIYYDataSource.m
//  Magpie
//
//  Created by 100TV protocal on 12-11-15.
//
//

#import "FIYYDataSource.h"
#import "FIHttpAPIHelper.h"
#import "FIRequest.h"

@implementation FIYYDataSource
static NSString* const KEY_REQUEST=@"!vanclrequest";
static NSString* const KEY_RESPONSEOBJ=@"!vanclresponseObj";

-(void) startRequest:(id<FIRequest>) arequest{
	assert(arequest!=nil);
	[arequest retain];
	id<FIYYRequest> vreq=(id<FIYYRequest>) arequest;
	NSMutableDictionary* dic2=[[NSMutableDictionary alloc] init];
	[dic2 addEntriesFromDictionary:[vreq params]];
	[dic2 setObject:vreq forKey:KEY_REQUEST];
	[FIHttpAPIHelper startLoad2:[vreq url] extraParams:dic2 object:self action:@selector(onLoaded:params:)];
	[dic2 release];
}

-(void) onLoaded:(id) response params:(NSDictionary*) params{
	id<FIYYRequest> request=[params objectForKey:KEY_REQUEST];
	assert(request!=nil);
	id responseObj=[request parseResponse:response];
	/*
     NSMutableDictionary* responseDic=[NSMutableDictionary dictionaryWithObjectsAndKeys:
     request,KEY_REQUEST,
     responseObj,KEY_RESPONSEOBJ,
     nil];
	 */
#ifndef NDEBUG
	//!! NSLog(@"request.target=%@\n  responseObj=%@",[request target],responseObj);
#endif
	[[request target] performSelector:[request action] withObject:responseObj];
	[request release];
}

static FIYYDataSource* m_dataSource=nil;

+(FIYYDataSource*) dataSource {
	@synchronized(self){
		static dispatch_once_t dt;
		dispatch_once(&dt, ^{
			m_dataSource=[[self alloc] init];
		} );
	}
	assert(m_dataSource!=nil);
	return m_dataSource;
}
@end







